﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using H2.Streams;
using System.IO;
using System.Xml;
using H2.DataTypes;

namespace Mystery
{
    public class Palette
    {
        public H2.DataTypes.Dependancy Item;

        public static void ReadWeapons(MapStream map, Palette[] palette)
        { Read(map, palette, 40); }

        public static void ReadVehicles(MapStream map, Palette[] palette)
        { Read(map, palette, 40); }

        public static void ReadEquipment(MapStream map, Palette[] palette)
        { Read(map, palette, 40); }

        public static void ReadScenery(MapStream map, Palette[] palette)
        { Read(map, palette, 40); }

        private static void Read(MapStream map, Palette[] palette, int chunkSize)
        {
            int startOffset = (int)map.Position;
            for (int i = 0; i < palette.Length; i++)
            {
                palette[i] = new Palette();
                palette[i].Item = map.ReadDependancyAt(startOffset + i * chunkSize, false);
            }
        }

        public static void WriteItemCollections(MapStream map, Palette[] palette)
        {
            for(int i = 0; i < palette.Length; i++)
            {
                map.Write(1); //Chunk Count
                map.Write((map.Position + 8) + map.Index.SecondaryMagic); //Pointer
                map.Write(0);//Unused Spawn Time
                map.Write(1.0f);//Weight
                map.Write(palette[i].Item);//Item
                map.Write(0);//StringID
            }
        }

        public static void OutputItemCollections(List<string> outputFiles, AISquad[] aiSquads, Palette[] palette, string outputDir, string prefix)
        {
            outputDir = Path.Combine(outputDir, prefix);
            Directory.CreateDirectory(outputDir);
            for (int i = 0; i < palette.Length; i++)
            {
                if (palette[i].Item.Path == "") continue;

                List<string> itemCollectionVariants = new List<string>();
                List<StringID> variants = new List<StringID>();
                itemCollectionVariants.Add(Path.GetFileName(palette[i].Item.Path));
                variants.Add(new StringID());
                foreach (AISquad squad in aiSquads)
                {
                    if (squad.Ignore) continue;
                    if (squad.VehicleIndex == i)
                    {
                        string variant = squad.VehicleVariant.ToString().TrimEnd('\0');
                        string itemCollectionName = Path.GetFileName(palette[i].Item.Path) + ((variant.Length > 0) ? "_" : "") + variant;
                        if (itemCollectionVariants.Contains(itemCollectionName) == false)
                        {
                            itemCollectionVariants.Add(itemCollectionName);
                            variants.Add(squad.VehicleVariant);
                        }
                    }
                }

                OutputItemCollections(outputFiles, variants, itemCollectionVariants, palette[i], outputDir, prefix);
            }
        }

        public static void OutputItemCollections(List<string> outputFiles, Placement[] placements, Palette[] palette, string outputDir, string prefix)
        {
            outputDir = Path.Combine(outputDir, prefix);
            Directory.CreateDirectory(outputDir);
            for (int i = 0; i < palette.Length; i++)
            {
                if (palette[i].Item.Path == "") continue;

                List<string> itemCollectionVariants = new List<string>();
                List<StringID> variants = new List<StringID>();
                itemCollectionVariants.Add(Path.GetFileName(palette[i].Item.Path));
                variants.Add(new StringID());
                foreach (Placement placement in placements)
                {
                    if (placement.TypeIndex == i)
                    {
                        string variant = placement.VariantName.ToString().TrimEnd('\0');
                        string itemCollectionName = Path.GetFileName(palette[i].Item.Path) + ((variant.Length > 0) ? "_" : "") + variant;
                        if (itemCollectionVariants.Contains(itemCollectionName) == false)
                        {
                            itemCollectionVariants.Add(itemCollectionName);
                            variants.Add(placement.VariantName);
                        }
                    }
                }

                OutputItemCollections(outputFiles, variants, itemCollectionVariants, palette[i], outputDir, prefix);
            }
        }

        private static void OutputItemCollections(List<string> outputFiles, List<StringID> variants, List<string> itemCollectionVariants, Palette paletteItem, string outputDir, string prefix)
        {
            for (int icn = 0; icn < itemCollectionVariants.Count; ++icn)
            {
                //Output Data
                string filename = Path.Combine(outputDir, itemCollectionVariants[icn]);

                if (outputFiles.Contains(filename)) continue;
                outputFiles.Add(filename);

                if (Directory.Exists(Path.GetDirectoryName(filename)) == false)
                    Directory.CreateDirectory(Path.GetDirectoryName(filename));

                FileStream fs = new FileStream(filename, FileMode.Create);
                BaseStream rs = new BaseStream(fs, null);
                rs.Write(1); //Chunk Count
                rs.Write(12); //Pointer
                rs.Write(0);//Unused Spawn Time
                rs.Write(100.0f);//Weight
                rs.Write(paletteItem.Item.Class);//Item Type
                rs.Write(0);//Item ID
                rs.Write(0);//StringID
                fs.Flush();
                fs.Close();

                string variant = variants[icn].ToString().TrimEnd('\0');

                //Output XML
                XMLInfo xml = new XMLInfo(filename + ".xml");
                xml.TagName = Path.Combine(prefix, Path.GetFileName(paletteItem.Item.Path) + ((variant.Length > 0) ? "_" : "") + variant);
                xml.TagClass = "itmc";
                xml.Size = 28;

                xml.WriteHeader();
                xml.WriteReflexive(0, 1, 16, 12);
                xml.WriteIdent(20, paletteItem.Item.ClassString, paletteItem.Item.Path);
                xml.WriteStringID(24, variant);
                xml.WriteEnd();
                xml.FlushAndClose();
            }
        }

        public override string ToString()
        { return Item.ToString(); }
    }
}